package com.zkingsoft.services.web.impl;

import com.zkingsoft.services.web.WebProductCategoryService;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.model.sys.SysFunction;
import com.zkingsoft.model.web.WebProductCategory;
import com.zkingsoft.dao.web.WebProductCategoryDao;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Map;

import com.zkingsoft.exception.GlobleException;
import com.zkingsoft.util.ModelUtils;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * This field was generated by Zking.software.Codegen.
 * @date 2017-03-15 14:22
 */
@Service("webProductCategoryService")
public class  WebProductCategoryServiceImpl implements    WebProductCategoryService  {

	
	@Autowired
	private WebProductCategoryDao webProductCategoryDao;
	
	
	@Override
	public int add(WebProductCategory webProductCategory){
		
		return webProductCategoryDao.insert(webProductCategory);
		
	}
	
	@Override
	public int batchAdd(List<WebProductCategory>  webProductCategoryList) {
		int num = 0;

		int c = 10000;
		int size = webProductCategoryList.size()/c + 1;
		for(int i=0; i<size; i++) {
			int begin = i*c;
			int end = (i+1)*c;
			end = end >= webProductCategoryList.size() ? webProductCategoryList.size() : end;
			List<WebProductCategory> insertList = webProductCategoryList.subList(begin, end);
			num += webProductCategoryDao.batchInsert(insertList);
		}
		return num;
		
	}
	
	
   	
    @Override
	public int modifyByMap(WebProductCategory oldWebProductCategory
	,WebProductCategory newWebProductCategory){
	
		if (!ModelUtils.isModified(oldWebProductCategory, newWebProductCategory)) {
			return 1;
		}
		Map<String, Object> modifyMap=null;
		try {
			modifyMap = ModelUtils.comparePojo2Map(oldWebProductCategory, newWebProductCategory);
		} catch (Exception e) {
			throw new GlobleException("数据对比失败");
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("prcId", oldWebProductCategory.getPrcId());
			webProductCategoryDao.updateByMap(modifyMap);
		}
		return 1;
	}
	
	@Override
	public int modifyByModel(WebProductCategory webProductCategory){
	
		return webProductCategoryDao.updateByModel(webProductCategory);
	
	}
	
	
	
	@Override
	public int remove(List<Long> list){
	
		return webProductCategoryDao.deleteByIds(list);
	
	}

/*	@Override
	public int removeById(Long prcId){
	
		return webProductCategoryDao.deleteById(prcId);
	
	}*/
	
	/**
	 * 删除功能和功能下的子功能
	 * 
	 * @param fnId
	 * @return
	 */
	@Override
	public int removeById(Long prcId) {
		if (prcId == null) {
			return 0;
		}
		List<Long> ids = new ArrayList<>();
		ids = getFunctionChirds(prcId, new ArrayList<Long>());
		return webProductCategoryDao.deleteByIds(ids);
	}
	
	/**
	 * 获取功能下的子功能id，包含功能自己的id
	 * 
	 * @param id
	 * @param ids
	 * @return
	 */
	private List<Long> getFunctionChirds(Long id, List<Long> ids) {
		WebProductCategory webProductCategory = new WebProductCategory();
		webProductCategory.setPrcParentId(id);
		// 获取子节点
		List<WebProductCategory> list = webProductCategoryDao.selectByModel(webProductCategory);
		if (!list.isEmpty()) {
			for (WebProductCategory webProductCategory1 : list) {
				// 删除子节点
				getFunctionChirds(webProductCategory1.getPrcId(), ids);
			}
		}
		ids.add(id);
		return ids;

	}

	
	@Override
	public int removeByModel(WebProductCategory webProductCategory){
	
		return webProductCategoryDao.deleteByModel(webProductCategory);
	
	}
	
	
	@Override
	public List<WebProductCategory> findInPage(WebProductCategory webProductCategory,  PaginationVO pageVo){
	
		return webProductCategoryDao.selectInPage(webProductCategory , pageVo);
	
	}
	
	@Override
	public List<WebProductCategory> findByModel(WebProductCategory webProductCategory){
	
		return webProductCategoryDao.selectByModel(webProductCategory);
	
	}
	
	@Override
	public int  findTotal(WebProductCategory webProductCategory){
	
		return webProductCategoryDao.selectTotalRecord(webProductCategory);
	
	}
	
	@Override
	public WebProductCategory  findById(Long prcId){
	
		return webProductCategoryDao.selectById(prcId);
	
	}

   	
	
	
}